﻿// ===============================================================================
// XCI.NET 西安交通信息WinForm开发框架 2018 版权所有
// ===============================================================================
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace XCI.Helper
{
    /// <summary>
    /// 输入法操作类
    /// </summary>
    /// <example>
    /// <code>
    /// using XCI.Helper;
    /// 
    /// Win32Helper.AllocConsole();//打开cmd窗口
    /// Console.Title = "输入法测试";
    /// 
    /// Console.WriteLine("输入法列表");
    /// var list = ImeHelper.GetImeList();//获取系统输入法数组
    /// foreach (string ime in list)
    /// {
    ///     Console.ForegroundColor = ConsoleColor.Green;
    ///     Console.WriteLine(ime);
    /// }
    /// Console.WriteLine("");
    /// 
    /// Console.ForegroundColor = ConsoleColor.Blue;
    /// Console.WriteLine("默认输入法名称:{0}", ImeHelper.GetDefaultIme());
    /// Console.WriteLine("");
    /// 
    /// Console.ForegroundColor = ConsoleColor.Red;
    /// Console.WriteLine("当前输入法名称:{0}",ImeHelper.GetCurrentIme());
    /// Console.WriteLine("");
    /// 
    /// Console.ForegroundColor = ConsoleColor.Yellow;
    /// Console.WriteLine("正在切换输入法。。。切换成 {0}", list[1]);
    /// var result = ImeHelper.SwtichIme(list[1]);//切换输入法
    /// Console.WriteLine(result ? "切换成功" : "切换失败");
    /// Console.WriteLine("");
    /// Console.ForegroundColor = ConsoleColor.Green;
    /// Console.WriteLine("当前输入法名称:{0}", ImeHelper.GetCurrentIme());
    /// </code>
    /// <img src="images/ImeHelper.jpg" />
    /// </example>
    public static class ImeHelper
    {
        /// <summary>
        /// 切换输入法
        /// </summary>
        /// <param name="imeName">输入法名称</param>
        /// <returns>切换成功返回true</returns>
        public static bool SwtichIme(string imeName)
        {
            if (String.IsNullOrEmpty(imeName))
            {
                return false;
            }

            foreach (InputLanguage language in InputLanguage.InstalledInputLanguages)
            {
                if (language.LayoutName != null && (!String.IsNullOrEmpty(language.LayoutName) && language.LayoutName.Equals(imeName)))
                {
                    InputLanguage.CurrentInputLanguage = language;
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 获取当前线程的输入法名称
        /// </summary>
        /// <returns>返回当前线程的输入法名称</returns>
        public static string GetCurrentIme()
        {
            return InputLanguage.CurrentInputLanguage.LayoutName;
        }

        /// <summary>
        /// 获取系统默认输入法名称
        /// </summary>
        /// <returns>返回统默认输入法名称</returns>
        public static string GetDefaultIme()
        {
            return InputLanguage.DefaultInputLanguage.LayoutName;
        }

        /// <summary>
        /// 获取输入法数组
        /// </summary>
        /// <returns>返回输入法数组</returns>
        public static string[] GetImeList()
        {
            List<string> imeList = new List<string>();
            for (int i = 0; i < InputLanguage.InstalledInputLanguages.Count; i++)
            {
                InputLanguage lang = InputLanguage.InstalledInputLanguages[i];
                imeList.Add(lang.LayoutName);
            }
            return imeList.ToArray();
        }

        /// <summary>
        /// 获取输入法数组
        /// </summary>
        /// <returns>返回输入法数组</returns>
        public static InputLanguage[] GetImeLists()
        {
            List<InputLanguage> imeList = new List<InputLanguage>();
            for (int i = 0; i < InputLanguage.InstalledInputLanguages.Count; i++)
            {
                InputLanguage lang = InputLanguage.InstalledInputLanguages[i];
                imeList.Add(lang);
            }
            return imeList.ToArray();
        }

        /// <summary>  
        /// 得到指定输入法的说明
        /// </summary>  
        /// <param name="ime">输入法</param>  
        /// <returns>输入法的说明</returns>  
        public static string GetImeDescription(InputLanguage ime)
        {
            int nBuffer = 0;
            nBuffer = NativeMethods.ImmGetDescription(ime.Handle, null, nBuffer);
            StringBuilder name = new StringBuilder(nBuffer);
            NativeMethods.ImmGetDescription(ime.Handle, name, nBuffer);
            string description = name.ToString();
            if (String.IsNullOrEmpty(description))
            {
                description = ime.LayoutName;
            }
            return description;
        }

        /// <summary>  
        /// 得到指定输入法的图标
        /// </summary>  
        /// <param name="ime">输入法</param>  
        /// <returns>输入法的图标 </returns>  
        public static Image GetImeBitmap(InputLanguage ime)
        {
            //int nBuffer = 0;
            Image oBitmap;

            //得到IME文件  
            //nBuffer = Win32Helper.ImmGetIMEFileName(ime.Handle, null, nBuffer);
            StringBuilder name = new StringBuilder();
            NativeMethods.ImmGetIMEFileName(ime.Handle, name, 255);

            if (String.IsNullOrEmpty(name.ToString())) return null;



            //从资源文件中得到图标  
            string sFileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), name.ToString());
            if (!File.Exists(sFileName)) return null;

            IntPtr smallIcon = (IntPtr)0, largeIcon = (IntPtr)0;
            try
            {
                NativeMethods.ExtractIconEx(sFileName, 0, ref largeIcon, ref smallIcon, 1);
                Icon ResIco = Icon.FromHandle(largeIcon);
                oBitmap = ResIco.ToBitmap();
            }
            finally
            {
                NativeMethods.DestroyIcon(largeIcon);
                NativeMethods.DestroyIcon(smallIcon);
            }
            return oBitmap;
        }

    }
}